﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	[Transaction(TransactionMode.Manual)]
	public class ComprhsivPipelineSec : IExternalCommand
	{
		Result IExternalCommand.Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
			Document document = activeUIDocument.Document;
			Transaction transaction = null;
			List<ViewType> viewTypes = new List<ViewType>
			{
				(ViewType)117,
				(ViewType)118
			};
			if (!YJKRevitTools.ViewCheck(activeUIDocument, viewTypes))
			{
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			FrmComprhsivPipelineSec frmComprhsivPipelineSec = new FrmComprhsivPipelineSec();
			frmComprhsivPipelineSec.ShowDialog();
			if (frmComprhsivPipelineSec.DialogResult == DialogResult.Cancel)
			{
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			OpearComprhsivPipelineSec opearComprhsivPipelineSec = new OpearComprhsivPipelineSec(document, frmComprhsivPipelineSec.TaggingSet);
			Result result;
			try
			{
				transaction = new Transaction(document, "管线综合剖面图");
				transaction.Start();
				opearComprhsivPipelineSec.Execute();
				transaction.Commit();
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception ex)
			{
				YJKMessageBox.Show(ex.Message);
				result = Autodesk.Revit.UI.Result.Failed;
			}
			finally
			{
				if (transaction != null && (int)transaction.GetStatus() != 3 && (int)transaction.GetStatus() != 2)
				{
					transaction.RollBack();
				}
			}
			return result;
		}
	}
}
